using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryManager : singleTon<InventoryManager>
{
    public class DragData
    {
        public SlotHolder originalHolder;
        public RectTransform originalParent;
    }
    [Header("Inventory Data")]
    public InventoryData_SO bagData;
    public InventoryData_SO actionData;
    public InventoryData_SO equipmentDataSaber;
    public InventoryData_SO shopData;
    public InventoryData_SO bagDataTemplate;
    public InventoryData_SO actionDataTemplate;
    public InventoryData_SO equipmentDataTemplateSaber;
    public InventoryData_SO shopDataTemplate;
    public Skill_List SkillData;
    public Skill_List skillDataTemplate;
    public InventoryData_SO equipmentDataArcher;
    public InventoryData_SO equipmentDataTemplateArcher;
    public InventoryData_SO equipmentDataMage;
    public InventoryData_SO equipmentDataTemplateMage;

    [Header("Contaniers")]
    public ContainerUI bagUI; 
    public ContainerUI actionUI;
    public ContainerUI equipmentUI;
    // public ContainerUI equipmentUIArcher;
    // public ContainerUI equipmentUIMage;
    public ContainerUI shopUI;
    public SkillContainerUI skillUI;

    [Header("Drag Canvas")] 
    public Canvas dragCanvas;

    public DragData currentDragData;

    private bool isOpen = true;
    private bool isOpenMap = true;
    public GameObject bag;
    public GameObject stats;
    public GameObject shop;
    public GameObject map;
    
    [Header("Tooltip")]
    public ItemToolTip itemToolTip;

    public ItemToolTip buyTip;
    
    


    public void SaveData()
    {
        SaveManager.Instance.Save(bagData,bagData.name);
        SaveManager.Instance.Save(actionData,actionData.name);
        SaveManager.Instance.Save(equipmentDataSaber,equipmentDataSaber.name);
        SaveManager.Instance.Save(equipmentDataArcher,equipmentDataArcher.name);
        SaveManager.Instance.Save(equipmentDataMage,equipmentDataMage.name);
        SaveManager.Instance.Save(SkillData,SkillData.name);
        SaveManager.Instance.Save(shopData,shopData.name);
    }

    public void LoadData()
    {
        SaveManager.Instance.Load(bagData,bagData.name);
        SaveManager.Instance.Load(actionData,actionData.name);
        SaveManager.Instance.Load(equipmentDataSaber,equipmentDataSaber.name);
        SaveManager.Instance.Load(equipmentDataArcher,equipmentDataArcher.name);
        SaveManager.Instance.Load(equipmentDataMage,equipmentDataMage.name);
        SaveManager.Instance.Load(SkillData,SkillData.name);
        SaveManager.Instance.Load(shopData,shopData.name);
        bagUI.RefreshUI();
        actionUI.RefreshUI();
        equipmentUI.RefreshUI();
        skillUI.RefreshUI();
    }

    protected override void Awake()
    {
        base.Awake();
        if (bagDataTemplate != null)
        {
            bagData = Instantiate(bagDataTemplate);
        }

        if (actionDataTemplate != null)
        {
            actionData = Instantiate(actionDataTemplate);
        }
        
        if (shopDataTemplate != null)
        {
            shopData = Instantiate(shopDataTemplate);
        }

        if (equipmentDataTemplateSaber != null)
        {
            equipmentDataSaber = Instantiate(equipmentDataTemplateSaber);
        }
        if (equipmentDataTemplateArcher != null)
        {
            equipmentDataArcher = Instantiate(equipmentDataTemplateArcher);
        }
        if (equipmentDataTemplateMage != null)
        {
            equipmentDataMage = Instantiate(equipmentDataTemplateMage);
        }
        if (skillDataTemplate != null)
        {
           SkillData = Instantiate(skillDataTemplate);
        }

        
    }

    private void Start()
    {
        bagUI.RefreshUI();
        actionUI.RefreshUI();
        equipmentUI.RefreshUI();
        skillUI.RefreshUI();
        shopUI.RefreshUI();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.B))
        {
            isOpen = !isOpen;
            bag.SetActive(isOpen);
            stats.SetActive(isOpen);
            bagUI.RefreshUI();
            actionUI.RefreshUI();
            equipmentUI.RefreshUI();
            skillUI.RefreshUI();
            shopUI.RefreshUI();
        }

        if (Input.GetKeyDown(KeyCode.M))
        {
            isOpenMap = !isOpenMap;
            map.SetActive(isOpenMap);
        }
        
        
    }

    #region 检测拖拽物品是否在Slot范围内

    public bool CheakBagUI(Vector3 position)
    {
        for (int i = 0; i < bagUI.slotHolders.Length; i++)
        {
            RectTransform t = (RectTransform) bagUI.slotHolders[i].transform;
            if (RectTransformUtility.RectangleContainsScreenPoint(t, position))
            {
                return true;
            }
        }
        return false;
    }
    public bool CheakActionUI(Vector3 position)
    {
        for (int i = 0; i < actionUI.slotHolders.Length; i++)
        {
            RectTransform t = (RectTransform) actionUI.slotHolders[i].transform;
            if (RectTransformUtility.RectangleContainsScreenPoint(t, position))
            {
                return true;
            }
        }
        return false;
    }
    public bool CheakEquipUI(Vector3 position)
    {
        for (int i = 0; i < equipmentUI.slotHolders.Length; i++)
        {
            RectTransform t = (RectTransform) equipmentUI.slotHolders[i].transform;
            if (RectTransformUtility.RectangleContainsScreenPoint(t, position))
            {
                return true;
            }
        }
        return false;
    }
    
    
    #endregion

    #region 检测任务物品

    public void CheckQuestItemInBag(string questItemName)
    {
        foreach (var item in bagData.items)
        {
            if (item.itemData != null)
            {
                if (item.itemData.itemName == questItemName)
                {
                    QuestManager.Instance.updateQuestProgress(item.itemData.itemName,item.amount);
                }
            }
        }
        
        foreach (var item in actionData.items)
        {
            if (item.itemData != null)
            {
                if (item.itemData.itemName == questItemName)
                {
                    QuestManager.Instance.updateQuestProgress(item.itemData.itemName,item.amount);
                }
            }
        }
    }

    #endregion

    public InventoryItem QuestItemInBag(ItemData_SO questItem)
    {
        return bagData.items.Find(i => i.itemData == questItem);
    }
    public InventoryItem QuestItemInAction(ItemData_SO questItem)
    {
        return actionData.items.Find(i => i.itemData == questItem);
    }
}
